package com.mf.page.menu;

import java.util.HashMap;
import java.util.List;
import java.util.Stack;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.mf.mpos.pub.Controler;
import com.mf.mpos.pub.CommEnum.CONNECTMODE;
import com.mf.page.ActionActivity;
import com.mf.R;

public class MainMenuPage extends ActionActivity {
	private ListView m_listView;
	List<HashMap<String, Object>> m_listData;
	Stack<String> m_prcoList = new Stack<String>();
	
	/*
	 * 创建菜单列表界面
	 */
	public static <activity> void ShowMenu(Activity activity , String id)
	{
		Intent intent = new Intent(activity, MainMenuPage.class);
		Bundle bundle = new Bundle();
		bundle.putString("id", id);
		intent.putExtras(bundle);
		activity.startActivity(intent);
	}
	
	// private List<String> data = new ArrayList<String>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		m_listView = new ListView(this);
		Bundle bundle = this.getIntent().getExtras();
		String id = bundle.getString("id");
		m_prcoList.clear();
		//获取要显示的菜单项，列表填充菜单项
		UpdateListView(id);
		setContentView(m_listView);
		MainMenuClickedRunnable.Instance();
		
		m_listView.setOnItemClickListener(OnListViewItemClickListener);   
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		//如果没有父层菜单，则关闭菜单的activity，否则，退回父级菜单
		if(m_prcoList.isEmpty()){
    		finish();
    	}
    	else{
    		String parent = m_prcoList.pop();
    		//刷新父级菜单
    		UpdateListView(parent);
    	}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}
	
	String title;
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		this.setTitle(title + modeinfo());		
		super.onResume();
	}

	String modeinfo(){
		String vl="(";
		vl+= Controler.GetMode() == CONNECTMODE.AUDIO ? "音频":"蓝牙";
		vl+="|";
		vl+= Controler.posConnected() ?  "已连接" : "未连接";
		vl+=")";
		
		return vl;
	}
	/*
	 * 刷新菜单列表界面
	 */
	private int UpdateListView(String id)
	{
		int ret = 0;
		List<HashMap<String, Object>> listdata;
		
		listdata = MainMenuData.Instance().getList(id);
		
		if(listdata.size() > 0){
		
			title =  MainMenuData.Instance().getTitle(id);			
			this.setTitle(title + modeinfo());
			
			m_listData = listdata;
			SimpleAdapter adapter = new SimpleAdapter(this,listdata,R.layout.menu_menulist,
					new String[]{"index" , "title"},
					new int[]{R.id.menu_index ,R.id.menu_title});
			m_listView.setAdapter(adapter);
			
			ret = 1;
		}
		return ret;
	}
	
	/*
	 * 菜单被选中处理
	 */
	private void SelectMenu(HashMap<String,Object> map)
	{
		int ret;
		String id = map.get("id").toString();
		String title = map.get("title").toString();
		//重置计数器
		resetTimer();
		
		//判断是否有子目录，有的话刷新菜单列表，显示子目录，并直接返回
		if(UpdateListView(id) == 1){
        	String parent = map.get("parent").toString();
            m_prcoList.push(parent);
            return ;
        }
		
		map.put("activity", this);
		MainMenuClickedRunnable.Instance().MenuClicked(map);
	}
	/*
	 * 列表点击响应
	 */
	OnItemClickListener OnListViewItemClickListener = new OnItemClickListener(){   
        @Override   
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {   
            //获得选中项的HashMap对象   
            HashMap<String,Object> map=(HashMap<String,Object>)m_listView.getItemAtPosition(arg2);   
            SelectMenu(map);
        }   
    };
    
    @Override
	protected void OnTimerOut() {
		// TODO Auto-generated method stub
		//super.OnTimerOut();
		//finish();
	}  
}
